#include<iostream>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<cstdio>
#include<unistd.h>

#define SIZE 4096
#define PathName "/home/hx"

using namespace std;

//获取唯一的key标识符
int Getkey(const char* pathname, int proj_id)
{
    int key = ftok(pathname, proj_id);
    if(key < 0)
    {
        perror("ftok errorn\n");
        exit(1);
    }
    return key;
}

//利用key创建共享内存，这个工作是由操作系统做的
int CreateshmidHelper(int flag)
{
    int key = Getkey(PathName, 0x7777);
    int shmid = shmget(key, SIZE, flag);
    if(shmid < 0)
    {
        perror("shmid crate\n");
        exit(2);
    }
    return shmid;
}

int Createshmid()
{
    return CreateshmidHelper(IPC_CREAT|IPC_EXCL|0666);  
}

int Getshmid()
{
    return CreateshmidHelper(IPC_CREAT);  
}